import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/Home.vue'
import Notice from '@/notice.vue'
import Tree from '@/tree.vue'
import About from '@/routePage/about.vue'
import List from '@/routePage/list.vue'
import ComponentGuard from '@/routePage/componentGuard.vue'
import axiosQuest from '../axios/index'

Vue.use(Router)

const router = new Router({
  // 模式：hash | history | abstract
  mode: 'history', 
  // 服务器地址，比如 http://localhost:8080/cart
  base: process.env.BASE_URL, 
})
// 获取路由列表
axiosQuest.ajax().then(routes=>{
  const routeConfig = routes.result.map(route => mapComponent(route));
  console.log(routeConfig)
  router.addRoutes(routeConfig);
})
// 映射关系
const compMap = {
  "Home": Home,
  "Notice": Notice,
  "Tree": Tree,
  "About": About,
  "List": List,
  "User": ()=>import("@/routePage/user.vue"),
  "ComponentGuard": ComponentGuard
}
// 递归替换
function mapComponent(route) {
  route.component = compMap[route.component];
  if(route.children) {
    route.children = route.children.map(child => mapComponent(child))
  }
  return route
}
export default router;